<?php

namespace app\api\controller\v1;

use app\api\model\Article as ArticleModel;
use app\common\controller\BaseController;

/**
 * 文章
 */

class Article extends BaseController
{

    /**
     * 首页文章
     * @explain 降序排序的推荐文章
     */
    public function index()
    {
        $article = ArticleModel::getByRecommend();
        $article ? $this->success('', compact('article')) : $this->error('没有数据');
    }

    /**
     * 文章详情
     */
    public function read()
    {
        $articleId = $this->notEmpty('article_id');
        $article = ArticleModel::find($articleId);
        if (!$article) {
            $this->notFoundData();
        }
        //推荐
        $recommend = ArticleModel::getByRecommend($articleId);
        //记录浏览量
        $res = $article->setInc('reading');
        if (!$res) {
            $this->forbidden();
        }
        $this->success('', ['article' => $article, 'recommend' => $recommend]);
    }

    /**
     * 全部文章
     */
    public function lists()
    {
        $size = $this->request->get('size');
        $articles = ArticleModel::getArticles($size);
        $articles->total() ? $this->success('', [
            'articles' => $articles->items(),
            'total' => $articles->total()
        ]) : $this->notFoundData();
    }
}
